package com.ychs.dpmms.controller;

import com.ychs.dpmms.entity.Branch;
import com.ychs.dpmms.entity.ResultMessage;
import com.ychs.dpmms.entity.SecondOrganization;
import com.ychs.dpmms.service.IBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0test
 * @date 2021/9/1
 */
@CrossOrigin
@RestController
@RequestMapping("/branch")
public class BranchController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource(name="branchService")
    private IBaseService<Branch> service;

    //添加方法
    @PostMapping("/add")
    public ResultMessage add(@Validated @RequestBody Branch branch, BindingResult br , Map map) {
        logger.debug("enter branch add");
        String msg = "增加成功";
        boolean result = false;
        /*校验不通过，会有错误信息*/
        if (br.hasFieldErrors()) {
            for (FieldError fe : br.getFieldErrors()){
                if (!fe.getField().equals("branch")) {
                    logger.debug("校验失败"+fe);
                    return new ResultMessage(false, null, "校验失败");
                }
            }
        }else{
            logger.debug("branch="+branch);

            //判断该二级党组织名称是否已经存在
            Map<String, Object> conditions = new HashMap<>();
            conditions.put("name", branch.getName());
            conditions.put("secondOrganization", branch.getSecondOrganization().getId());
            int count = service.searchTotalNumber(conditions);
            //若查到该名称的数量大于等于1，则不添加，并返回信息，否则继续执行添加
            if(count>=1){
                msg = "该名称已经存在！";
            }else{
                result = service.add(branch);
            }
        }
        logger.debug("branch add result = "+ result);
        ResultMessage resultMessage = new ResultMessage(result, branch, msg);
        return resultMessage;
    }

    //修改方法
    @PostMapping("/modify")
    public ResultMessage modify(@Validated @RequestBody Branch branch, BindingResult br , Map map) {
        logger.debug("enter branch modify");
        String msg = "修改成功";
        boolean result = false;
        /*校验不通过，会有错误信息*/
        if (br.hasFieldErrors()) {
            for (FieldError fe : br.getFieldErrors()){
                if (!fe.getField().equals("branch")) {
                    logger.debug("校验失败"+fe);
                    return new ResultMessage(false, null, "校验失败");
                }
            }
        }else{
            logger.debug("branch="+branch);

            //判断该二级党组织名称是否已经存在
            Map<String, Object> conditions = new HashMap<>();
            conditions.put("name", branch.getName());
            conditions.put("notId", branch.getId());
            conditions.put("secondOrganization", branch.getSecondOrganization().getId());
            int count = service.searchTotalNumber(conditions);
            //  若查到该名称的数量大于等于1，则不添加，并返回信息，否则继续执行添加
            if(count>=1){
                msg = "该名称已经存在！";
            }else{
                result = service.modify(branch);
            }
        }
        logger.debug("branch modify result = "+ result);
        ResultMessage resultMessage = new ResultMessage(result, branch, msg);
        return resultMessage;
    }

    @GetMapping("/search")
    public ResultMessage search(Integer secondOrganization, Integer limit, Integer page) {
        Map<String, Object> conditions = new HashMap<>();
        if (secondOrganization != null ) {
            conditions.put("secondOrganization", secondOrganization);
        }
        int count = service.searchTotalNumber(conditions);

        if(limit!=null&&page!=null){
            conditions.put("begin", (page - 1) * limit);
            conditions.put("pageSize", limit);
        }

        List<Branch> branchList = service.search(conditions);
        logger.debug("branchList=" + branchList.toString());
        ResultMessage resultMessage = new ResultMessage(true, branchList, "" + count);
        return resultMessage;
    }

    @PostMapping("/remove/{id}")
    public ResultMessage remove(@PathVariable("id") int id){
        logger.debug("enter branch remove");
        logger.debug("id="+id);
        boolean result = service.remove(id);
        logger.debug("result = " + result);
        ResultMessage resultMessage =
                new ResultMessage(result, null, "删除成功");
        return resultMessage;
    }
}
